/*
moxl - Modular XML Library
Copyright (c) 2007, Malcolm Sparks

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
package moxl.dom.visitor.support;

import moxl.dom.MoxlElement;
import moxl.dom.MoxlNode;
import moxl.dom.MoxlNodeVisitor;
import moxl.dom.Visitable;

public class ElementVisitableSupport
{
	private static final ElementVisitableSupport INSTANCE = new ElementVisitableSupport();

	private ElementVisitableSupport()
	{
	}

	public void accept(MoxlNodeVisitor visitor, MoxlElement element, Iterable<? extends MoxlNode> children)
	{
		visitor.startNode(element);
		if (children != null)
		{
			for (MoxlNode node : children)
			{
				if (node instanceof Visitable)
				{
					Visitable visitable = (Visitable) node;
					visitable.accept(visitor);
				}
				else
				{
					throw new IllegalStateException("Child element not visitable in visitable element, child type is " + node.getClass().getName());
				}
			}
		}
		visitor.endNode(element);
	}

	public static ElementVisitableSupport getInstance()
	{
		return INSTANCE;
	}

}
